package god.services;

// 컨트롤 + 쉬프트 + O - import 안되어 있는 애들 모두 자동 생성

import god.interfaces.J24ProfessorDAO;
import god.model.J24ProfessorVO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class J24ProfessorDAOImpl implements J24ProfessorDAO {

	private static String DRV = "oracle.jdbc.driver.OracleDriver";
	private static String URL = "jdbc:oracle:thin:godraines/godraines@112.216.253.124:1521:xe";
	private static String insertSQL = "";
	private static String selectSQL = " select * from professor order by pfno ";
	private static String selectOneSQL = "";
	private static String updateSQL = "";
	private static String deleteSQL = "";
	
	@Override
	public String[] insertProfessor(J24ProfessorVO pvo) {
		return null;
	}

	// DAO에서 출력하면 String 으로 해도 되지만, 하나의 기능만 하도록 하기 위해 동적 배열로 ServiceImpl로 넘겨준다
	@Override
	public ArrayList<J24ProfessorVO> selectProfessor() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList<J24ProfessorVO> pvoes = null;
		
		try {
			conn = makeConnection();
			pstmt = conn.prepareStatement(selectSQL);
			rs = pstmt.executeQuery();
			pvoes = new ArrayList<>();

			// 한 레코드를 담아 놓음
			while ( rs.next() ) {
				J24ProfessorVO vo = new J24ProfessorVO();
					vo.setPfno(rs.getString(1));
					vo.setPfname(rs.getString(2));
					vo.setFields(rs.getString(3));
					vo.setSkills(rs.getString(4));
					vo.setRegdate(rs.getString(5));
				pvoes.add(vo);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		} finally {
			closeConnection(conn, pstmt, rs);
		}
		return pvoes;
	}

	@Override
	public ArrayList<J24ProfessorVO> selectOneProfessor(String pfno) {
		return null;
	}

	@Override
	public String[] updateProfessor(String pfno) {
		return null;
	}

	@Override
	public String[] deleteProfessor(String pfno) {
		return null;
	}
	
	public Connection makeConnection() throws Exception {
		Connection conn = null;
		Class.forName(DRV);
		conn = DriverManager.getConnection(URL);
		return conn; 
		}
	
	public void closeConnection(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		if (rs != null) try { rs.close(); } catch(Exception ex) {}
		if (pstmt != null) try { pstmt.close(); } catch(Exception ex) {}
		if (conn != null) try { conn.close(); } catch(Exception ex) {}
	}

}
